﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Configuration;

namespace Exportation.ExportationTypes
{
    public class ExcelExportation : IExportation
    {
        public String Export(ExpenseReport xr)
        {
            //verificar se já foi exportado, se já foi retorna o path
            StringBuilder sb = new StringBuilder();
            sb.Append(ConfigurationManager.AppSettings["exportsDest"]);
            sb.Append("ExpenseExportation-");
            sb.Append(xr.Id);
            sb.Append(".xls");
            string fileName = sb.ToString();
            string sheetName = xr.Name;
            DAL.ExcelGenerator.GenerateBinaryXls(this.GetLines(xr),
                typeof(ExportacionExcelLine),
                fileName, sheetName, GetSourceDictionary());
            //fileName = ConfigurationManager.AppSettings["exportsDest"];
            return fileName;
        }

        private System.Collections.IList GetLines(ExpenseReport xr)
        {
            List<ExportacionExcelLine> list = new List<ExportacionExcelLine>();
            using (DataAccessDataContext da = new DataAccessDataContext())
            {
                
                foreach (ExpenseLine line in xr.ExpenseLines)
                {
                    string type = line.ExpenseType.Name;
                    string description = line.Description;
                    string value = Convert.ToString(line.Value);
                    string date = Convert.ToString(line.Date);
                    list.Add(new ExportacionExcelLine(type, description, value, date));
                }
            }
            return list;
        }

        public static Dictionary<string, string> GetSourceDictionary()
        {
            //Translation dictionary
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Type", "Tipo");
            dict.Add("Date", "Data");
            dict.Add("Description", "Descrição");
            dict.Add("Value", "Valor");

            return dict;
        }
    }
    class ExportacionExcelLine
    {
        string type, description, value, date;

        public ExportacionExcelLine(string type, string description, string value, string date)
        {
            this.type = type;
            this.description = description;
            this.value = value;
            this.date = date;
        }

        public string Date
        {
            get { return date; }
            set { date = value; }
        }

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

    }
}
